package com.qb.devserv;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.xuexiang.xui.XUI;
import com.xuexiang.xui.utils.XToastUtils;
import com.xuexiang.xui.widget.toast.XToast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import Adapter.ItemAdapter;
import Adapter.TypeAdapter;
import utils.AppUtil;
import utils.DevInfoUtil;
import utils.MyUtil;

public class HardwareActivity extends AppCompatActivity {

    Context ctx;
    private RecyclerView rvType,rvItem;
    JSONArray typeData,itemData;
    SharedPreferences SP;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        XUI.initTheme(this);
        XToast.Config.get().setGravity(Gravity.CENTER);
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_hardware);
        Window window = this.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        this.getWindow().setAttributes(lp);
        ctx = HardwareActivity.this;
        SP = getSharedPreferences("dev_mgt_setting", this.MODE_PRIVATE);
        rvType = findViewById(R.id.rv_type);
        rvItem = findViewById(R.id.rv_item);

        dataInit();
        //getDevInfo();
    }

    Boolean isTypeClick = false;
    private void initAdapter() {
        LinearLayoutManager typeLLM = new LinearLayoutManager(this);
        typeLLM.setOrientation(LinearLayoutManager.VERTICAL);
        rvType.setLayoutManager(typeLLM);
        TypeAdapter typeAdapter = new TypeAdapter(this, typeData);
        rvType.setAdapter(typeAdapter);

        LinearLayoutManager itemLLM = new LinearLayoutManager(this);
        itemLLM.setOrientation(LinearLayoutManager.VERTICAL);
        rvItem.setLayoutManager(itemLLM);
        ItemAdapter itemAdapter = new ItemAdapter(this, itemData);
        rvItem.setAdapter(itemAdapter);
        // 左联右
        typeAdapter.setOnItemClickListener(new TypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                isTypeClick = true;
                int first = itemLLM.findFirstCompletelyVisibleItemPosition();
                int last = itemLLM.findLastCompletelyVisibleItemPosition();
                MyUtil.showLog("first:"+first,"last:"+last+",itemlength:"+itemData.length());
                int top = 0;
                if (position == 0) {
                    top = 0;
                    rvItem.smoothScrollToPosition(0);
                } else if (position == 1) {
                    if (first > 4) {
                        rvItem.smoothScrollToPosition(4);
                    } else {
                        top  = rvItem.getChildAt(4).getTop();
                    }
                } else if (position == 2) {
                    if (first > 11) {
                        rvItem.smoothScrollToPosition(11);
                    } else if ( last < 11 ) {
                        rvItem.smoothScrollToPosition(20);
                    } else {
                        top  = rvItem.getChildAt(last - 11).getTop();
                    }
                } else if (position == 3) {
                    if (last < 19) {
                        rvItem.smoothScrollToPosition(19);
                    } else {
                        top  = rvItem.getChildAt(last - 19).getTop();
                    }
                } else {
                    top = 0;
                    rvItem.smoothScrollToPosition(itemData.length()-1);
                }
                if (top > 0) rvItem.smoothScrollBy(0, top);
            }
        });
        // 右联左
        rvItem.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE){
                    if (isTypeClick) {
                        isTypeClick = false;
                    } else {
                        int first = itemLLM.findFirstCompletelyVisibleItemPosition();
                        int last = itemLLM.findLastCompletelyVisibleItemPosition();
                        MyUtil.showLog("first:"+first,"last:"+last+",itemlength:"+itemData.length());
                        if (first <= 1) typeAdapter.selectedEffect(0);
                        else if (last == itemData.length()-1) typeAdapter.selectedEffect(typeData.length() - 1);
                        else if (first >= 4 &&  first < 9) typeAdapter.selectedEffect(1);
                        else if (first >= 10 &&  first <= 11) typeAdapter.selectedEffect(2);
                        else if (first > 11 &&  first <= 18) typeAdapter.selectedEffect(3);
                    }

                }
            }
        });
    }

    public void dataInit() {
        String[] type = {"设备信息","终端","屏幕","网络","蓝牙"};
        JSONObject o;
        try {
            // type
            typeData = new JSONArray();
            for (int i=0;i<type.length;i++) {
                o = new JSONObject();
                int icon = R.drawable.icon_baseline_info_32;
                if (i==0) icon = R.drawable.icon_baseline_info_32;
                if (i==1) icon = R.drawable.icon_baseline_devices_32;
                if (i==2) icon = R.drawable.icon_baseline_desktop_mac_32;
                if (i==3) icon = R.drawable.icon_baseline_wifi_32;
                if (i==4) icon = R.drawable.icon_baseline_bluetooth_32;
                o.put("icon",icon);
                o.put("label",type[i]);
                typeData.put(o);
            }
            // item
            itemData = new JSONArray();
            o = new JSONObject();
            o.put("key","title");
            o.put("value",type[0]);
            itemData.put(o);
            o = new JSONObject();
            o.put("key","所属商户");
            String clientName = SP.getString("clientName","");
            o.put("value",MyUtil.isNull(clientName)?"请在管理平台进行出厂设置":clientName);
            itemData.put(o);
            o = new JSONObject();
            o.put("key","设备型号");
            String model = SP.getString("model","");
            o.put("value",MyUtil.isNull(model)?"请在管理平台进行出厂设置":model);
            itemData.put(o);
            o = new JSONObject();
            o.put("key","出厂序列号");
            String sn = SP.getString("sn","");
            if (MyUtil.isNull(sn)) {
                startActivities(new Intent[]{new Intent().setClass(ctx, FactoryActivity.class)});
                finish();
            } else {
                o.put("value",sn);
            }
            itemData.put(o);
            o = new JSONObject();
            o.put("key","title");
            o.put("value",type[1]);
            itemData.put(o);
            o = new JSONObject();
            o.put("key","主板序列号");
            o.put("value",DevInfoUtil.getSN());
            itemData.put(o);
            o = new JSONObject();
            o.put("key","处理器");
            o.put("value",DevInfoUtil.getModel());
            itemData.put(o);
            o = new JSONObject();
            o.put("key","运行内存(可用/总)");
            String ram = DevInfoUtil.getRAM(ctx).getString("avail")+"/"+DevInfoUtil.getRAM(ctx).getString("total");
            o.put("value",ram);
            itemData.put(o);
            o = new JSONObject();
            o.put("key","存储(可用/总)");
            String rom = DevInfoUtil.getROM(ctx).getString("avail")+"/"+DevInfoUtil.getROM(ctx).getString("total");
            o.put("value",rom);
            itemData.put(o);
            o = new JSONObject();
            o.put("key","Android版本");
            o.put("value",DevInfoUtil.getOsInfo());
            itemData.put(o);
            o = new JSONObject();
            o.put("key","系统版本号");
            o.put("value",DevInfoUtil.getFingerPrint());
            itemData.put(o);
            o = new JSONObject();
            o.put("key","title");
            o.put("value",type[2]);
            itemData.put(o);
            JSONArray screenInfo = DevInfoUtil.getScreenInfo(ctx);
            for(int i=0;i<screenInfo.length();i++) {
                o = new JSONObject();
                if (i==0) o.put("key","主屏");
                else if (i==1)  o.put("key","副屏");
                else o.put("key","副屏"+i);
                o.put("value","分辨率"+screenInfo.get(i).toString());
                itemData.put(o);
            }
            o = new JSONObject();
            o.put("key","title");
            o.put("value",type[3]);
            itemData.put(o);
            JSONArray netType = DevInfoUtil.getNetworkType(ctx);
            for(int i=0;i<netType.length();i++) {
                JSONObject netTYpe_o = new JSONObject(netType.get(i).toString());
                if (!MyUtil.isNull(netTYpe_o)) {
                    o = new JSONObject();
                    o.put("key","网络类型");
                    o.put("value",netTYpe_o.getString("type"));
                    itemData.put(o);
                    o = new JSONObject();
                    o.put("key","MAC地址");
                    o.put("value",netTYpe_o.getString("mac"));
                    itemData.put(o);
                }
            }
            JSONArray localIp = DevInfoUtil.getLocalIP();
            for(int i=0;i<localIp.length();i++) {
                JSONObject localIp_o = new JSONObject(localIp.get(i).toString());
                if (!MyUtil.isNull(localIp_o)) {
                    o = new JSONObject();
                    o.put("key","局域网类型");
                    o.put("value",localIp_o.getString("type"));
                    itemData.put(o);
                    o = new JSONObject();
                    o.put("key","ipv6地址");
                    o.put("value",localIp_o.getString("ipv6"));
                    itemData.put(o);
                    o = new JSONObject();
                    o.put("key","ipv4地址");
                    o.put("value",localIp_o.getString("ipv4"));
                    itemData.put(o);
                }
            }
            o = new JSONObject();
            o.put("key","title");
            o.put("value",type[4]);
            itemData.put(o);
            JSONObject bleInfo = DevInfoUtil.getBleInfo(ctx);
            o = new JSONObject();
            o.put("key","蓝牙名称");
            o.put("value",bleInfo.getString("name"));
            itemData.put(o);
            o = new JSONObject();
            o.put("key","蓝牙MAC地址");
            o.put("value",bleInfo.getString("mac"));
            itemData.put(o);
//            o = new JSONObject();
//            o.put("key","蓝牙版本");
//            o.put("value",bleInfo.getString("version"));
//            itemData.put(o);
            initAdapter();
        } catch (JSONException e) {
            e.printStackTrace();
            XToastUtils.error("设备信息获取失败 "+e.getMessage(), Toast.LENGTH_LONG);
        }
    }

    public void getDevInfo() {
        String s = "********** DevInfo ************"
                +"\nSN:"+ DevInfoUtil.getSN()
                +"\ngetModel:"+ DevInfoUtil.getModel()
                +"\ngetBrand:"+ DevInfoUtil.getBrand()
                +"\ngetOsInfo:"+ DevInfoUtil.getOsInfo()
                +"\ngetFingerPrint:"+ DevInfoUtil.getFingerPrint()
                +"\ngetCpuINfo:"+ DevInfoUtil.getCpuInfo("Hardware")
                //+"\ngetCpuModel:"+ DevInfoUtil.getCpuModel()
                +"\ngetRAM:"+ DevInfoUtil.getRAM(ctx)
                +"\ngetROM:"+ DevInfoUtil.getROM(ctx)
                +"\n************ ScreenInfo *************"
                +"\ngetScreenInfo:"+ DevInfoUtil.getScreenInfo(ctx)
                +"\n************ NetWork *************"
                +"\ngetNetworkType:"+ DevInfoUtil.getNetworkType(ctx)
                +"\ngetLocalIP:"+ DevInfoUtil.getLocalIP()
                +"\ngetBleInfo:"+ DevInfoUtil.getBleInfo(ctx)
                +"\n************ END *************";
        MyUtil.showLog("getDevInfo",s);
        String a = String.valueOf(AppUtil.getAppList(ctx));
        MyUtil.showLog("getDevInfo app",a);
    }
}